package util

import (
	"io"
	"net"
)

func IOTransport(src io.ReadWriteCloser, dst io.ReadWriteCloser) {
	e1 := make(chan interface{}, 1)
	e2 := make(chan interface{}, 1)
	go func() {
		_, err := io.Copy(dst, src)
		e1 <- err
	}()
	go func() {
		_, err := io.Copy(src, dst)
		e2 <- err
	}()
	select {
	case <-e1:
	case <-e2:
	}
}

func Closer(rws ...interface{}) {
	for _, rw := range rws {
		if rw == nil {
			continue
		}
		if v, ok := rw.(net.Conn); ok {
			v.Close()
			continue
		}
		if v, ok := rw.(io.ReadWriteCloser); ok {
			v.Close()
			continue
		}
		if v, ok := rw.(net.Listener); ok {
			v.Close()
			continue
		}
	}
}
